package at.fhhgb.mc.android.mfb_hgb.activity;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import at.fhhgb.mc.android.mfb_hgb.R;
import at.fhhgb.mc.android.mfb_hgb.model.Offer;

public class FragmentOverviewAdapter extends ArrayAdapter<Offer> {
	// internal list of the relevant data
	List<Offer> mList;
	final Context c;

	public FragmentOverviewAdapter(Context _context, int _textViewResourceId, List<Offer> _objects) {
		super(_context, _textViewResourceId, _objects);
		c = _context;
		mList = _objects;
	}

	@Override
	public View getView(int _position, View _convertView, ViewGroup _parent) {
		if (_convertView == null) {
			Context c = getContext();
			LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			_convertView = inflater.inflate(R.layout.fragment_overview_listitem, null);
		}

		final Offer element = mList.get(_position);

		if (element != null) {
			TextView v = null;
			v = (TextView) _convertView.findViewById(R.id.f_overview_offertype);
			switch ((int) element.getType()) {
			case 1:
				v.setText(_convertView.getResources().getString(R.string.offertype_offer));
				break;

			case 2:
				v.setText(_convertView.getResources().getString(R.string.offertype_search));
				break;
			}

			v = (TextView) _convertView.findViewById(R.id.f_overview_src);
			v.setText(element.getSource());

			v = (TextView) _convertView.findViewById(R.id.f_overview_dst);
			v.setText(element.getDestination());

			if(element.getOver().isEmpty())
			{
				((LinearLayout)_convertView.findViewById(R.id.overview_listitem_layout_over)).setVisibility(LinearLayout.GONE);
			}
			else
			{
				v = (TextView) _convertView.findViewById(R.id.f_overview_stops);
				v.setText(element.getOver());
			}

			v = (TextView) _convertView.findViewById(R.id.f_overview_time_from);
			String s = element.getDepartureFrom().toString();
			v.setText(s.substring(0, s.length() - 5));	
			
			if(element.getDepartureTo() == null)
			{
				((LinearLayout)_convertView.findViewById(R.id.overview_listitem_layout_timeTo)).setVisibility(LinearLayout.GONE);
			}
			else
			{
				v = (TextView) _convertView.findViewById(R.id.f_overview_time_to);
				s = element.getDepartureTo().toString();
				v.setText(s.substring(0, s.length() - 5));
			}
					

			v = (TextView) _convertView.findViewById(R.id.f_overview_author);
			v.setText(String.valueOf(element.getName()));
			
			
			
			
			if(element.getNote() != null && element.getNote().length() > 0)
			{
				v = (TextView) _convertView.findViewById(R.id.f_overview_note);
				v.setText(element.getNote());
			}
			else
			{
				((LinearLayout)_convertView.findViewById(R.id.overview_listitem_layout_note)).setVisibility(LinearLayout.GONE);
			}

		} // if element
		return _convertView;
	}
}